package com.system.controller;

import com.system.Tools.TimeTools;
import com.system.Tools.UserTools;
import com.system.function.object.Delivery;
import com.system.function.object.User;
import com.system.function.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/delivery")
public class DeliveryController {

    @Autowired
    private DeliveryService DeliveryService;
    @Autowired
    private TimeTools timeTools;
    @Autowired
    private UserTools userTools;


    //查询
    @RequestMapping("/s")
    @ResponseBody
    public Map<String,Object> s(Delivery Delivery) {
        List<Delivery> allDelivery = DeliveryService.getAllDelivery(Delivery);
        Map<String,Object> returnMap  = new HashMap<>();
        returnMap.put("total",DeliveryService.getCountDelivery(Delivery));
        returnMap.put("rows",allDelivery);
        return returnMap;
    }
    //查询
    @RequestMapping("/select")
    @ResponseBody
    public List<Delivery> select(Delivery Delivery) {
        List<Delivery> allDelivery = DeliveryService.getAllDelivery(Delivery);
        return allDelivery;
    }

    //添加
    @RequestMapping("/add")
    @ResponseBody
    public String add(Delivery Delivery,HttpServletRequest request) {
        User userMessageForSeesion = userTools.getUserMessageForSeesion(request);
        Delivery.setSaveUser(userMessageForSeesion.getName());

        return DeliveryService.addDelivery(Delivery);
    }

    //修改
    @RequestMapping("/up")
    @ResponseBody
    public String up(Delivery Delivery) {
        return DeliveryService.updateDelivery(Delivery);
    }

    //修改
    @RequestMapping("/examineUp")
    @ResponseBody
    public String updateExamine(HttpServletRequest request, Delivery Delivery) {
        User userMessageForSeesion = userTools.getUserMessageForSeesion(request);
        Delivery.setExamineUser(userMessageForSeesion.getName());
        Delivery.setExamineTime(timeTools.getTime());

        return DeliveryService.updateExamine(Delivery);
    }

    //用户删除
    @RequestMapping("/del")
    @ResponseBody
    public String del(Delivery Delivery) {
        return DeliveryService.delDelivery(Delivery);
    }


}
